define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    $("#shangjia").click(function (){
        var checkedbox= $("#table").bootstrapTable('getSelections'); 	//获取选中的数据
        var leng = checkedbox.length;
        if(leng > 0){
            Layer.confirm(__("sure_action"), {
                title: "Tips",
                icon: 0,
                btn: ['Yes'],
                btnAlign: "r",
            }, function(index, layero){
                var idsArr = [];
                for(var i=0;i<leng;i++){
                    var oneItem = checkedbox[i];
                    idsArr.push(oneItem.id);
                }
                var ids = idsArr.join(',');
                var url = "content/article/uparticle?ids="+ids;

                var index_load = layer.load();
                $.get(url,function (res) {
                    Layer.close(index_load);
                    if(res.code == 1){
                        Layer.msg(res.msg);
                        window.location.reload();
                    }else{
                        Layer.msg(res.msg);
                    }

                });
            });

        }else{
            Layer.msg(__('Please select record'));
        }
    });
    var Controller = {
        index: function () {
            // 初始化表格参数配置

            var window_innerheight = window.innerHeight;
            var table_height = window_innerheight-20;
            Table.api.init({
                extend: {
                    index_url: 'content/article/index',
                    add_url: 'content/article/add',
                    edit_url: 'content/article/edit',
                    del_url: 'content/article/del',
                    multi_url: '',
                    dragsort_url: '',
                    table: 'article',
                },
                pageSize: Config.pagesize,
                // height: table_height
            });

            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate: false},
                        {field: 'title', title: __('Name'),operate: 'like'},


                        {field: 'type', title: __('Type'), searchList: {"1":"服务条款","2":"常见问题","3":"通知公告"}, formatter: Controller.api.formatter.setType},
                        {field: 'status', title: __('Status'), operate: false, formatter: Controller.api.formatter.setStatus},

                        {field: 'create_time', title: __('Create time'), operate: false,formatter: Controller.api.formatter.setRiqi},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);
            $(".btn-add").data("area", ["60%", "80%"]);
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area", ["60%", "80%"]);
            });
            // 为表格绑定事件
            Table.api.bindevent(table);


            //必须默认触发shown.bs.tab事件
            // $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");

        },
        add: function () {
            Controller.api.bindevent();
        },
        uparticle: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        del: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                setRiqi:function (value, row, index){
                    if(value  == ''){
                        return '-';
                    }else{
                        return value.substr(0,10);
                    }
                },
                setStatus:function (value, row, index) {
                    if(row.status == -1){
                        var html = '<span class="text-error"><i class="fa fa-circle"></i>'+__('Disabled')+'</span>';
                    }else if(row.status == 1){
                        var html = '<span class="text-success"><i class="fa fa-circle"></i>'+__('Enable')+'</span>';
                    }else{
                        var html = '<span class="text-danger"><i class="fa fa-circle"></i>'+__('Unknown')+'</span>';//6
                    }
                    return html;
                },
                showThumb:function (value, row, index) {
                    return '<a class="" href="'+row.thumb+'" target="_blank"><img src="'+row.thumb+'" alt="" style="width: auto;height: 50px;"></a>';
                },
                setType:function (value, row, index) {
                    if(row.type == 1){
                        var html = '<span class="label label-success">服务条款</span>';
                    }else if(row.type == 2){
                        var html = '<span class="label label-default">常见问题</span>';
                    }else{
                        var html = '<span class="label label-info">通知公告</span>';
                    }
                    return html;
                }
            }
        }
    };
    return Controller;
});
